package com.scholarship.modules.backend.controller;

import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.modules.backend.dao.Teacher;
import com.scholarship.modules.backend.service.TeacherAuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhz
 * @date 2021/8/4 19:49
 * 备注：关于教师账号的一些接口
 */
@Api(tags = {"关于教师账号的一些接口"})
@RestController
public class TeacherAuthorityController {
    @Autowired
    TeacherAuthorityService teacherAuthorityService;

    @ApiOperation("获取所有教师账号及其权限 @author zhz")
    @GetMapping("/admin/listAllTeachers")
    public RetResult listAllTeachers(){
        return RetResponse.makeRsp(RetCode.SUCCESS,"成功展示所有教师账号",teacherAuthorityService.listAllTeachers());
    }

    @ApiOperation("创建教师账号及其权限 @author zhz")
    @PostMapping("/admin/creatTeacher")
    public RetResult creatTeacher(@RequestBody Teacher teacher){
        try {
            if(teacherAuthorityService.creatTeacher(teacher))
                return RetResponse.makeRsp(RetCode.SUCCESS,"成功创建教师账号");
            else
                return RetResponse.makeRsp(RetCode.FAIL,"创建教师账号失败，请检查账号是否重复");
        }catch (Exception e){
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"创建教师账号失败，请联系开发人员 ");
        }
    }

    @ApiOperation("更改教师账号密码 @author zhz")
    @PostMapping("/admin/changeTeacherPwd")
    public RetResult changeTeacherPwd(String userId, String newPwd){
        try{
            if(teacherAuthorityService.changeTeacherPwd(userId,newPwd)){
                return RetResponse.makeOKRsp();
            }else {
                return RetResponse.makeRsp(RetCode.FAIL,"不能对教师以外的账号进行修改");
            }
        } catch (Exception e) {
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
        }
    }

    @ApiOperation("删除教师账号 @author zhz")
    @PostMapping("/admin/deleteTeacher")
    public  RetResult deleteTeacher(String userId){
        try{
            teacherAuthorityService.deleteTeacher(userId);
            return RetResponse.makeOKRsp();
        }catch (Exception e){
            return RetResponse.makeRsp(RetCode.FAIL,"删除账号失败");
        }
    }
}
